# MEDIPS batch processing:
# Performing MEDIPS.methylationProfiling for pre-processed data

library(MEDIPS)
library(BSgenome.Hsapiens.UCSC.hg19)

getwd()
# [1] "/usr/local/data/Data/NCH/MBD-Seq/MEDIP-Analysis"

# get list of files in directory
# here we're loading the stored results of the previous batch run
files <- list.files(pattern="glioma.*RData$")

# profiling of the whole genome
chrom <- "chr"

#####################################
# beginning of loop
#####################################

#loop through files, process and write results to individual RData files 
for (i in files) {
	sample <- gsub(".RData","",i)
	print(sample)
	
	# loading the RData file
	load(i)
	
	# profile complete data set
  output <- sample
  profile <- MEDIPS.methylProfiling(data1 = MEDIPS.SET, frame_size=500, step=250, math = median, select = 2)
  # write profiling results to CSVpst
  write.csv(profile, file=paste(output,"profile.csv", sep="."))
  gc()
  #system("if [ -d processed ]; then mv ")
}


	
###########################################
# end of loop
###########################################

